package com.itheima.sc.impl.ums;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.sc.api.ums.UmsRoleApi;
import com.itheima.sc.mapper.ums.AdminRoleRelationMapper;
import com.itheima.sc.mapper.ums.UmsRoleMapper;
import com.itheima.sc.mapper.ums.UmsRoleResourceRelationMapper;
import com.itheima.sc.pojo.ums.AdminRoleRelation;
import com.itheima.sc.pojo.ums.UmsRole;
import com.itheima.sc.pojo.ums.UmsRoleMenuRelation;
import com.itheima.sc.pojo.ums.UmsRoleResourceRelation;
import com.itheima.sc.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@DubboService
public class UmsRoleServiceImpl implements UmsRoleApi {
    @Autowired
    private UmsRoleMapper umsRoleMapper;
    @Autowired
    private UmsRoleResourceRelationMapper umsRoleResourceRelationMapper;

    /**
     * 添加角色
     */
    @Autowired
    private AdminRoleRelationMapper adminRoleRelationMapper;

    @Override
    public void createRole(UmsRole umsRole) {
        umsRoleMapper.insert(umsRole);
    }

    /**
     * 删除角色
     */
    @Override
    public void deleteByIds(Integer[] ids) {
        ArrayList<Integer> idList = CollUtil.toList(ids);
        umsRoleMapper.deleteBatchIds(idList);
    }

    /**
     * 获取所有角色
     */
    @Override
    public List<UmsRole> listAll() {
        LambdaQueryWrapper<UmsRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(UmsRole::getCreateTime);
        return umsRoleMapper.selectList(wrapper);
    }

    /**
     * 给角色分配资源
     */
    @Override
    public void allocResource(Integer resourceId, Integer roleId) {
        UmsRoleResourceRelation umsRoleResourceRelation = new UmsRoleResourceRelation();
        umsRoleResourceRelation.setResourceId(Convert.toLong(resourceId));
        umsRoleResourceRelation.setRoleId(Convert.toLong(roleId));
        umsRoleResourceRelationMapper.insert(umsRoleResourceRelation);
    }

    /**
     * 根据角色名称分页获取角色列表
     */
    @Override
    public PageResult list(Integer pageNum, Integer pageSize, String keyword) {

        Page<UmsRole> page = new Page(pageNum, pageSize);

        //构建查询条件
        LambdaQueryWrapper<UmsRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(keyword), UmsRole::getName, keyword);
        wrapper.orderByDesc(UmsRole::getCreateTime);
        umsRoleMapper.selectPage(page, wrapper);

        //封装分页数据
        PageResult pageResult = new PageResult();
        pageResult.setPageSize(pageSize);
        pageResult.setPageNum(pageNum);
        pageResult.setTotal(Convert.toInt(page.getTotal()));
        pageResult.setTotalPage(Convert.toInt(page.getPages()));
        pageResult.setList(page.getRecords());

        //返回结果
        return pageResult;
    }

    @Override
    public List<UmsRole> selectByAdminId(Long adminId) {
        LambdaQueryWrapper<AdminRoleRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminRoleRelation::getAdminId, adminId);
        List<AdminRoleRelation> relations = adminRoleRelationMapper.selectList(wrapper);
        List<UmsRole> roles = new ArrayList<>();
        for (AdminRoleRelation relation : relations) {
            UmsRole umsRole = umsRoleMapper.selectById(relation.getRoleId());
            roles.add(umsRole);
        }
        return roles;
    }

    /**
     * 调用API修改数据
     */
    @Override
    public void updateStatus(Integer id, Integer status) {
        UmsRole umsRole = new UmsRole();
        umsRole.setStatus(status);
        umsRole.setId(Convert.toLong(id));
        umsRoleMapper.updateById(umsRole);
    }

    @Override
    public UmsRole getUmsRole(Long roleId) {
        //构建查询条件
        LambdaQueryWrapper<UmsRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsRole::getId,roleId);
        //查询数据并返回
        return umsRoleMapper.selectOne(wrapper);
    }

    @Override
    public void update(UmsRole umsRole) {
        umsRoleMapper.updateById(umsRole);
    }

    /**
     *根据roleIds查询角色
     */
    @Override
    public List<UmsRole> getByIds(List<Long> roleIds) {
        return umsRoleMapper.selectBatchIds(roleIds);
    }
}
